package dhy.lyb.change.controller;

import dhy.lyb.change.enums.MessageEnum;
import dhy.lyb.change.model.ExtendModel;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.*;

/**
 * 公共controller类
 * Created by Administrator on 2016/11/22.
 */
@Controller
public class BaseController {

    @Autowired
    private ExtendModel extendModel;

    /**
     * 文件上传
     *
     * @param file
     * @param request
     */
    public String uploadFile(MultipartFile file, HttpServletRequest request, String url) {
        System.out.println("------------------------BEGIN-------------------------");
        String path = request.getSession().getServletContext().getRealPath(url);
        String fileName = file.getOriginalFilename();
        File targetFile = new File(path, fileName);
        if (!targetFile.exists()) {
            targetFile.mkdirs();
        }
        //保存
        try {
            file.transferTo(targetFile);
        } catch (Exception e) {
            e.printStackTrace();
        }
        //position
        System.out.println(fileName + "保存路径：" + path);
        System.out.println(fileName + "文件名" + fileName);
        System.out.println(fileName + "项目下的路径" + path);
        System.out.println("------------------------OVER-------------------------");
        return path;
    }

    /**
     * 文件下载
     *
     * @param fileName
     * @param url
     * @param request
     * @param response
     */
    public void downloadFile(String fileName, String url, HttpServletRequest request, HttpServletResponse response) {
        try {
            String path = request.getSession().getServletContext().getRealPath(url);
            System.out.println(path);
            InputStream inputStream = new FileInputStream(new File(path
                    + File.separator + fileName));

            OutputStream os = response.getOutputStream();
            byte[] b = new byte[2048];
            int length;
            while ((length = inputStream.read(b)) > 0) {
                os.write(b, 0, length);
            }
            os.close();
            inputStream.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /**
     * 判断文件类型是否符合要求
     * 文档上传类型：.txt
     *
     * @param file
     */
    public boolean getBookFileType(MultipartFile file) {
        boolean flag = false;
        System.out.println("getBookType:============================");
        if (!file.isEmpty()) {
            String suffixList = "txt";
            String fileName = file.getOriginalFilename();
            String suffix = fileName.substring(fileName.lastIndexOf(".") + 1, fileName.length());
            if (suffixList.contains(suffix.trim().toLowerCase())) {
                System.out.println(suffix);
                flag = true;
            }
        }
        return flag;
    }

    /**
     * 判断文件类型是否符合要求
     * 图片上传类型：.jpg .jpeg .png .bmp
     *
     * @param file
     */
    public boolean getLogoFileType(MultipartFile file) {
        boolean flag = false;
        System.out.println("getLogoType:============================");
        if (!file.isEmpty()) {
            String suffixList = "jpg,jpeg,png,bmp";
            String fileName = file.getOriginalFilename();
            String suffix = fileName.substring(fileName.lastIndexOf(".") + 1, fileName.length());
            if (suffixList.contains(suffix.trim().toLowerCase())) {
                System.out.println(suffix);
                flag = true;
            }
        }
        return flag;
    }

    /**
     * 成功返回标记
     *
     * @return
     */
    public ExtendModel successResult(int pk) {
        extendModel.setReturnCode("000");
        extendModel.setReturnMsg(MessageEnum.OPERATE_SUCCESS.getDescription());
        extendModel.setPk(pk);
        return extendModel;
    }

    /**
     * 失败返回标记
     *
     * @return
     */
    public ExtendModel errorResult() {
        extendModel.setReturnCode("111");
        extendModel.setReturnMsg(MessageEnum.OPERATE_ERROR.getDescription());
        return extendModel;
    }

    /**
     * 发送数据错误返回标记
     *
     * @return
     */
    public ExtendModel requestErrorResult() {
        extendModel.setReturnCode("101");
        extendModel.setReturnMsg(MessageEnum.JSON_ACCESS_ERROR.getDescription());
        return extendModel;
    }

    /**
     * 自定义返回标记
     *
     * @param value
     * @return
     */
    public ExtendModel insResult(int value, int pk) {
        extendModel.setReturnCode("100");
        extendModel.setReturnMsg(MessageEnum.getValue(value).getDescription());
        extendModel.setPk(pk);
        return extendModel;
    }
}
